/**
 * listener for sell
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class SellListener implements ActionListener {
	/**
	 * Method toString.
	
	 * @return String */
	@Override
	public String toString() {
		return "SellListener [p=" + planet + ", good=" + good + ", mps=" + mps
				+ ", gameInfo=" + gameInfo + ", index=" + index + "]";
	}

	/**
	 * Field planet.
	 */
	private final Planet planet;

	/**
	 * Field good.
	 */
	private final String good;

	/**
	 * Field mps.
	 */
	private final MarketPlaceScreen mps;

	/**
	 * Field gameInfo.
	 */
	private final GameInfo gameInfo;

	/**
	 * Field index.
	 */
	private int index=0;

	/**
	 * Constructor for SellListener.
	 * @param p Planet
	 * @param good String
	 * @param info GameInfo
	 */
	public SellListener(Planet p, String good, GameInfo info) {
		// TODO Auto-generated constructor stub
		this.planet=p;
		this.good=good;
		mps=new MarketPlaceScreen();
		gameInfo=info;
		for (int i=0;i   < GoodInfo.GoodName.length;i++){

			if (GoodInfo.GoodName[i].equals(good)) index=i;
		}
	}

	/**
	 * Method actionPerformed.
	 * @param arg0 ActionEvent
	
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (!canMatchTechLevel()) {
			mps.upDate("The tech level is too low");
		    return;
		}
		if (!canMatchOwn()) {
			mps.upDate("You don't own this good");
		    return;
		}
		PlayerInfo.sell(index, GoodInfo.getMODELEDPRICE()[index]);
		updateLabels();
	}

	/**
	 * Method updateLabels.
	 */
	private void updateLabels(){
		
		MarketPlaceScreen.Money.setText("" + PlayerInfo.getMoney());
		MarketPlaceScreen.Space.setText("" + PlayerInfo.getCargoAvailable());
		for (int i=0;i < GoodInfo.GoodName.length;i++){
			MarketPlaceScreen.Stock[i].setText("" + PlayerInfo.getStock(i) + "");
		}
		MarketPlaceScreen.Money.setText( " " + PlayerInfo.getMoney());
		MarketPlaceScreen.Space.setText ("" + PlayerInfo.getCargoAvailable());
		for (int i = 0;i < GoodInfo.GoodName.length;i++){
			MarketPlaceScreen.Stock[i].setText("" + PlayerInfo.getStock(i) + "");
		}
	}

	/**
	 * Method checkTechLevel.
	
	 * @return boolean */
	public boolean canMatchTechLevel(){
		return (planet.getSolarSystem().getTechLevel() >= GoodInfo.getMP()[index]);
	}

	/**
	 * Method checkOwn.
	
	 * @return boolean */
	public boolean canMatchOwn(){
    	return (PlayerInfo.getStock(index) > 0);
    }
}

